using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataTransferObject;

namespace DataAccess
{
    public class clsMuonTapChiDAO:clsDataAccessHelper
    {

        // Fields
        private String tenTable;

        // Contructors
        public clsMuonTapChiDAO()
        {
            this.tenTable = "MUONTAPCHI";
        }

        #region Methods
        //-------------------------------------Lấy dữ liệu bảng Mượn Tạp Chí-----------------------

        public DataTable LayTapChiDocGiaMuon(clsTapChiDTO obj, clsDocGiaDTO DocGia)
        {
            String sql;
            DataTable dt;
            try
            {
                sql = "SELECT D.MATHE,T.MATAPCHI,TENTAPCHI,NGAYMUON,HOTEN,GIOITINH,TENLDG,NGAYLAPTHE,NGAYHETHAN,TONGNO,HINHANH";
                sql += " FROM TAPCHI T,MUONTAPCHI M,LOAIDG L,DOCGIA D";
                sql += " WHERE T.MATAPCHI=M.MATAPCHI";
                sql += " AND L.MALDG=D.MALDG";
                sql += " AND D.MATHE=M.MATHE";
                sql += " AND T.MATAPCHI LIKE '" + obj.MaTapChi + "'";
                sql += " AND D.MATHE='" + DocGia.MaThe + "'";

                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayDanhSachTapChiDocGiaMuon(clsDocGiaDTO obj)
        {
            String sql;
            DataTable dt;
            try
            {
                sql = "SELECT D.MATHE,T.MATAPCHI,TENTAPCHI,NGAYMUON,HOTEN,GIOITINH,TENLDG,NGAYLAPTHE,NGAYHETHAN,TONGNO,HINHANH";
                sql += " FROM TAPCHI T,MUONTAPCHI M,LOAIDG L,DOCGIA D";
                sql += " WHERE T.MATAPCHI=M.MATAPCHI";
                sql += " AND L.MALDG=D.MALDG";
                sql += " AND D.MATHE=M.MATHE";
                sql += " AND D.MATHE LIKE '" + obj.MaThe + "'";

                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable SelectAll()
        {
            String sql;
            DataTable dt;
            try
            {
                sql = "SELECT * FROM " + this.tenTable;
                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //-----------------------------------------------------------------------------//

        //-------------------------------------Thêm vào bảng Mượn Tạp Chí-----------------------

        public bool Insert(clsMuonTapChiDTO muontapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "Set dateformat dmy INSERT INTO " + this.tenTable;
                sql += " values('";
                sql += muontapchi.MaTapChi + "','";
                sql += muontapchi.MaThe + "','";
                sql += muontapchi.NgayMuon.Day + "/" + muontapchi.NgayMuon.Month + "/" +muontapchi.NgayMuon.Year + "')";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        //-----------------------------------------------------------------------------//

        //-------------------------------------Xóa vào bảng Mượn Tạp Chí-----------------------
        public bool Delete(clsMuonTapChiDTO muontapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "DELETE ";
                sql += " FROM " + this.tenTable;
                sql += " WHERE MATAPCHI LIKE '" + muontapchi.MaTapChi + "'";
                sql += " AND MATHE LIKE '" + muontapchi.MaThe + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }
        //-----------------------------------------------------------------------------//


        //-------------------------------------Sửa vào bảng Mượn Tạp Chí-----------------------

        public bool Update(clsMuonTapChiDTO muontapchi)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "Set dateformat dmy UPDATE " + this.tenTable;
                sql += " SET";
                sql += " NGAYMUON = '";
                sql += muontapchi.NgayMuon + "'";
                sql += " WHERE MATAPCHI LIKE '";
                sql += muontapchi.MaTapChi + "'";
                sql += " AND MATHE LIKE '";
                sql += muontapchi.MaThe + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        //-----------------------------------------------------------------------------//


        #endregion


    }
}
